//访问用户媒体设备的兼容方法
function getUserMedia(constraints, success, error) {
    if (navigator.mediaDevices.getUserMedia) {
        //最新的标准API
        navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
    } else if (navigator.webkitGetUserMedia) {
        //webkit核心浏览器
        navigator.webkitGetUserMedia(constraints, success, error)
    } else if (navigator.mozGetUserMedia) {
        //firfox浏览器
        navigator.mozGetUserMedia(constraints, success, error);
    } else if (navigator.getUserMedia) {
        //旧版API
        navigator.getUserMedia(constraints, success, error);
    }
}

let video = document.getElementById('video');
let canvas = document.getElementById('canvas');
let context = canvas.getContext('2d');

function success(stream) {
    //兼容webkit核心浏览器
    let CompatibleURL = window.URL || window.webkitURL;
    //将视频流设置为video元素的源
    console.log(stream);

    //video.src = CompatibleURL.createObjectURL(stream);
    video.srcObject = stream;
    video.play();
}

function error(error) {
    console.log(`访问用户媒体设备失败${error.name}, ${error.message}`);
}

if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
    //调用用户媒体设备, 访问摄像头
    getUserMedia({video: {width: 480, height: 320}}, success, error);
} else {
    alert('不支持访问用户媒体');
}

document.getElementById("btn_snap").addEventListener("click", function () {
    context.drawImage(video, 0, 0, 480, 320);
    var imgData = canvas.toDataURL();
    var base64Data = imgData.substr(22);
    $.ajax({
        url: '/aip/identify',
        type: "POST",
        data: {image: base64Data},
        dataType: 'json',
        success: function (data) {
            if (data.result != null && data.result.user_list[0].score >= 80) {
                alert("我认出来啦！你是:" + data.result.user_list[0].user_id + ", " + data.result.user_list[0].user_info);
            } else {
                alert("不好意思，没认出来，快去注册吧！\n" + data.error_msg);
            }
            console.log(data);
        }
    });
});


$(document).ready(function (e) {
    $("#btn_delete").click(function () {
        var uid = $("#uid").val();
        var group = $("#group").val();
        $.ajax({
            url: "/aip/delete",
            type: 'post',
            data: {uid: uid, group: group},
            dataType: 'json',
            success: function (data) {
                if (data.error_msg != null) {
                    alert(data.error_msg);
                } else {
                    alert("删除成功");
                }

            }
        });
    })
    getGroup();
});

function getGroup(group) {
    $.ajax({
        url: "/aip/find",
        type: 'post',
        data: {group: group},
        dataType: 'json',
        success: function (data) {
            if (group == null) {
                var tbody = "";
                for (var i = 0; i < data.result_num; i++) {
                    var tr = "<tr>";
                    tr += "<td>" + (i + 1) + "</td>";
                    tr += "<td><a href='#'>" + data.result[i] + "</a></td>";
                    tr += "</tr>";
                    tbody += tr;
                }
                $('#Table thead').empty();
                var thead = "<tr>" + "<th width='50'>序号</th>" + "<th width='150'>组名</th>" + "</tr>";
                $('#Table thead').append(thead);
                $('#Table tbody').empty();
                $('#Table').children('tbody').append(tbody);
                $("#Table a").each(function (i) {
                    $(this).click(function () {
                        var group = $(this).text();
                        getGroup(group);
                    })
                })
            } else {
                var tbody = "";
                $.each(data.result, function (index, el) {
                    var tr = "<tr>";
                    tr += "<td>" + (index + 1) + "</td>";
                    tr += "<td>" + el.uid + "</td>";
                    tr += "<td>" + el.user_info + "</td>";
                    tr += "</tr>";
                    tbody += tr;
                })
                $('#Table thead').empty();
                var thead = "<tr>" + "<th width='50'>序号</th>" + "<th width='150'>用户名</th>" + "<th width='150'>用户信息</th>" + "</tr>";
                $('#Table thead').append(thead);
                $('#Table tbody').empty();
                $('#Table').children('tbody').append(tbody);
            }

        }
    });
}